#!/bin/bash

# 温の家 - Sealos 快速部署脚本
# 一键部署到 https://bja.sealos.run/

echo "🚀 温の家 - Sealos 快速部署"
echo "================================"

# 检查 Docker
if ! command -v docker &> /dev/null; then
    echo "❌ 请先安装 Docker"
    exit 1
fi

# 检查 kubectl
if ! command -v kubectl &> /dev/null; then
    echo "❌ 请先安装 kubectl"
    exit 1
fi

echo "✅ 环境检查通过"

# 构建镜像
echo "🔨 构建 Docker 镜像..."
docker build -f Dockerfile.production -t sealos.hub:5000/wenno-app:latest .

if [ $? -ne 0 ]; then
    echo "❌ 镜像构建失败"
    exit 1
fi

echo "✅ 镜像构建成功"

# 推送镜像
echo "📤 推送镜像到 Sealos Hub..."
docker push sealos.hub:5000/wenno-app:latest

if [ $? -ne 0 ]; then
    echo "❌ 镜像推送失败"
    exit 1
fi

echo "✅ 镜像推送成功"

# 部署应用
echo "🚀 部署到 Kubernetes..."
kubectl apply -f sealos-deploy.yaml

if [ $? -ne 0 ]; then
    echo "❌ 部署失败"
    exit 1
fi

echo "✅ 部署成功"

# 等待部署完成
echo "⏳ 等待部署完成..."
kubectl rollout status deployment/wenno-app --timeout=300s

echo ""
echo "🎉 部署完成！"
echo "📱 访问地址: https://bja.sealos.run"
echo "🏥 健康检查: https://bja.sealos.run/health"
echo ""
echo "🔧 管理命令:"
echo "  查看状态: kubectl get pods -l app=wenno-app"
echo "  查看日志: kubectl logs -l app=wenno-app -f"
echo "  重启服务: kubectl rollout restart deployment/wenno-app"
echo ""
echo "👤 测试账号:"
echo "  用户1: user1 / 123456"
echo "  用户2: user2 / 123456"
